from langchain_community.chat_models import ChatTongyi

import os

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

os.environ["DASHSCOPE_API_KEY"] = "sk-9d8f1914800e497f8717144e860f99bc"
llm = ChatTongyi()
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
chain = prompt | llm | StrOutputParser()

# chain的输出结果放在joke里面
analysis_prompt = ChatPromptTemplate.from_template("is this a funny joke? {joke}")

# 连个chain组合
composed_chain = {"joke": chain} | analysis_prompt | llm | StrOutputParser()
res = composed_chain.invoke({"topic": "bears"})
print(res)
